package com.unme.controller;


import com.alibaba.fastjson.JSONObject;
import com.unme.model.User;
import com.unme.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/api")
public class COSController {


    @Autowired
    private UserService userService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/addUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> addUser(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        User user=new User();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            user.setOpenid(jsonParam.getString("openid"));
            user.setAge(jsonParam.getInteger("age"));
            user.setMark(jsonParam.getString("mark"));
            user.setNick(jsonParam.getString("nick"));
            userService.addUser(user);
            map.put("message","数据添加成功！");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","数据添加失败！");
            return map;

        }
    }


    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/findAllUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> findAllUser(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        User user=new User();
        Map<String,Object> map=new HashMap<>();
        String oa=request.getParameter("oa");
        try{
            map.put("data",userService.findAllUser(null));
            map.put("message","数据查询成功！");
            return map;
        }catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据查询失败！");
            return map;
        }
        }
        @CrossOrigin(origins = "*", maxAge = 3600)
        @RequestMapping(value = "/deleteUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
        @ResponseBody
        public Map<String,Object> deleteUser(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

            User user=new User();
            Map<String,Object> map=new HashMap<>();
            String oa=request.getParameter("oa");
            try{
                user.setOpenid(jsonParam.getString("openid"));
                user.setAge(jsonParam.getInteger("age"));
                user.setMark(jsonParam.getString("mark"));
                user.setNick(jsonParam.getString("nick"));
                userService.deleteUser(user);
                map.put("message","数据删除成功！");
                return map;
            }catch (Exception e){
                e.printStackTrace();
                map.put("message","数据删除失败！");
                return map;

            }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> updateUser(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        User user = new User();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            user.setOpenid(jsonParam.getString("openid"));
            user.setAge(jsonParam.getInteger("age"));
            user.setMark(jsonParam.getString("mark"));
            user.setNick(jsonParam.getString("nick"));
            userService.updateUser(user);
            map.put("message", "数据更新成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据更新失败！");
            return map;

        }
    }
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getUserById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> getUserById(@RequestBody(required=true) JSONObject jsonParam, HttpServletResponse httpResponse, HttpServletRequest request) {

        User user = new User();
        Map<String, Object> map = new HashMap<>();
        String oa = request.getParameter("oa");
        try {
            user.setOpenid(jsonParam.getString("openid"));
            user.setAge(jsonParam.getInteger("age"));
            user.setMark(jsonParam.getString("mark"));
            user.setNick(jsonParam.getString("nick"));
            userService.getUserById(user);
            map.put("message", "数据获取成功！");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "数据获取失败！");
            return map;

        }
    }
}
